package com.yykj.tencent.im.server.controller;

import com.yykj.common.vo.PageData;
import com.yykj.common.vo.ResultVO;
import com.yykj.tencent.im.server.bean.request.*;
import com.yykj.tencent.im.server.bean.response.*;
import com.yykj.tencent.im.server.dto.GroupSuppleDTO;
import com.yykj.tencent.im.server.exception.TimException;
import com.yykj.tencent.im.server.form.*;
import com.yykj.tencent.im.server.service.GroupMessageService;
import com.yykj.tencent.im.server.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@Slf4j
@Api(tags = "腾讯云通信--群组管理")
@RequestMapping("api/tencent/im/group")
public class GroupController {
    @Resource
    private GroupService groupService;
    @Resource
    private GroupMessageService groupMessageService;


    @SneakyThrows
    @ApiOperation("获取App中的所有群组")
    @GetMapping("/list")
    public GetAppidGroupListResult list(GetAppidGroupListRequest body){
        return groupService.getAppIdGroupList(body);
    }


    @ApiOperation("获取群成员详细资料")
    @PostMapping("/member/info")
    public GetGroupMemberInfoResult memberInfo(@RequestBody GetGroupMemberInfoRequest body){
        return groupService.getGroupMemberInfo(body);
    }

    @ApiOperation("创建群组")
    @GetMapping("/create")
    @Deprecated
    public CreateGroupResult create(
            @RequestParam(name = "ownerAccount") String ownerAccount,
            @RequestParam(name = "type",required = false) String type,
            @RequestParam(name = "name") String name,
            @RequestParam(name = "faceUrl") String faceUrl
            ) throws TimException {
        CreateGroupRequest body = new CreateGroupRequest();
        body.setType(type);
        body.setOwnerAccount(ownerAccount);
        body.setName(name);
        body.setFaceUrl(faceUrl);
        return groupService.createGroup(body);
    }

    @ApiOperation("创建群组")
    @PostMapping("/create")
    public CreateGroupResult create(@RequestBody CreateGroupForm body) throws TimException {
        CreateGroupRequest form = new CreateGroupRequest();
        form.setType(body.getType());
        form.setOwnerAccount(body.getOwnerAccount());
        form.setName(body.getName());
        form.setFaceUrl(body.getFaceUrl());
        return groupService.createGroup(form);
    }

    @SneakyThrows
    @ApiOperation("在群组中发送系统通知")
    @PostMapping("/send/notify")
    public SendGroupSystemNotificationResult sendGroupNotify(@RequestBody SendGroupSystemNotificationRequest body){
        return groupService.sendGroupSystemNotification(body);
    }

    @SneakyThrows
    @ApiOperation("在群组中发送普通消息")
    @PostMapping("/send/message")
    public SendGroupMsgResult sendGroupMsg(@RequestBody SendGroupMsgRequest body){
        return groupService.sendGroupMsg(body);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群组ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "补充资料类型：Video/Custom/Sound/Image/File/Custom", required = true, dataType = "String"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "Integer"),
    })
    @ApiOperation("获取补充资料")
    @GetMapping("/supple")
    public ResultVO<PageData<GroupSuppleDTO>> pageSupple(
            @RequestParam(name = "groupId") String groupId,
            @RequestParam(name = "type") String type,
            @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ){
        GroupSuppleForm form = GroupSuppleForm.builder()
                .groupId(groupId)
                .type(type)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();
        return ResultVO.success(groupMessageService.listGroupSupple(form));
    }

    @ApiOperation("获取补充资料")
    @PostMapping("/supple")
    public ResultVO<PageData<GroupSuppleDTO>> pageSupple(@RequestBody GroupSuppleForm body){
        return ResultVO.success(groupMessageService.listGroupSupple(body));
    }

    @ApiOperation("上传补充资料")
    @PostMapping("/supple/upload")
    public ResultVO<GroupSuppleDTO> uploadSelfSupple(@RequestBody SelfSuppleForm body){
        return groupMessageService.uploadSelfSupple(body);
    }

    @ApiOperation("增加群成员")
    @PostMapping("/member/add")
    public AddGroupMemberResult memberAdd(@RequestBody AddGroupMemberForm form){
        return groupService.addGroupMember(form);
    }

    @ApiOperation("删除群成员")
    @PostMapping("/member/delete")
    public DeleteGroupMemberResult memberDelete(@RequestBody DeleteGroupMemberForm form){
        return groupService.deleteGroupMember(form);
    }

}
